<?php

defined('BASEPATH') or die('No direct script access allowed');

/**
 * 账户
 * @author  Karson
 * @name    account.php
 * @since   2012-12-2 23:37:51
 */
class Account_Controller extends Controller {

    function _init() {
        check_level();
        $this->account->load_user_account();
    }

    function index() {
        $css = array('cash' => 'b', 'bank' => 'g', 'credit' => 'o', 'elec' => 'b', 'other' => 'w');
        $unit = array('cash' => '个', 'bank' => '张', 'credit' => '张', 'elec' => '个', 'other' => '个');
        $account_list = array();
        $account = get_account_list();
        $account_type = $this->account->account_type;
        foreach ($account_type as $m => $n) {
            $account_list[$m] = array('name' => $n, 'css' => $css[$m], 'unit' => $unit[$m], 'list' => array());
        }
        foreach ($account as $m => $n) {
            $account_list[$n['type']]['list'][] = $n;
        }
        $this->view->assign("account_list", $account_list);
        render("account/index");
    }

    function log($id, $page = 1, $pagesize = 10) {
        $id = (int) $id;
        $page = (int) $page;
        $pagesize = (int) $pagesize;
        $result = $this->db->one("SELECT COUNT(*) AS num FROM {pre}account_log WHERE user_id='{$this->auth->user_id}' AND account_id='{$id}'");
        $total = $result['num'];
        $offset = ($page - 1) * $pagesize;
        $pagelist = $this->page->get("/account/log/{$id}/{page}/{$pagesize}/", $total, $page, $pagesize, false, false);

        $list = array();
        $this->db->query("SELECT * FROM {pre}account_log WHERE user_id='{$this->auth->user_id}' AND account_id='{$id}' ORDER BY id DESC LIMIT {$offset},{$pagesize}", "list");
        while ($row = $this->db->get_array("list")) {
            $list[] = $row;
        }
        $data = array("pagelist" => $pagelist, 'list' => $list);
        $this->view->assign($data);
        render("account/log");
    }

    function add() {
        $type = $this->input->get("type");
        $domid = $this->input->get("domid");
        if ($this->form->is_submit()) {
            $currency_id = $this->input->post("currency_id");
            $name = $this->input->post("name");
            $balance = $this->input->post("balance");
            $type = $this->input->post("type");
            $value = array(
                'uuid' => get_uuid(), 'currency_id' => $currency_id, 'user_id' => $this->auth->user_id,
                'name' => $name, 'createtime' => TIME,
                'balance' => 0,
                'type' => $type,
                'modifytime' => TIME, 'state' => 1
            );
            $bank_id = $this->input->post("bank_id");
            if ($bank_id) {
                $bankinfo = $this->db->one("SELECT * FROM {pre}bank WHERE id='{$bank_id}'");
                if ($bankinfo) {
                    $value['bank'] = $bankinfo['name'];
                    $value['icon'] = $bankinfo['icon'];
                }
            }
            $billdatelast = $this->input->post("billdatelast");
            $repaydatelast = $this->input->post("repaydatelast");
            $billdate = (int) $this->input->post("billdate");
            $repaydate = (int) $this->input->post("repaydate");
            $value['billdate'] = $billdatelast ? 0 : $billdate;
            $value['repaydate'] = $repaydatelast ? 0 : $repaydate;
            $value['id'] = $this->db->insert("{pre}account", $value);
            $value['domid'] = $this->input->post("domid");
            $this->account->load_user_account();
            $this->account->update_user_account($value['id'], $balance, "建立账户[{$value['name']}],ID[{$value['id']}]");
            $result = array('info' => '提交成功！', 'status' => 'y', 'data' => $value);
            echo json_encode($result);
            exit;
        }
        $this->view->assign("type", $type);
        $this->view->assign("domid", $domid);
        render("account/add");
    }

    function edit($id) {
        $account = $this->account->get_account($id);
        if ($account) {
            if ($this->form->is_submit()) {
                $father_id = $this->input->post("father_id");
                $name = $this->input->post("name");
                $balance = $this->input->post("balance");
                $memo = $this->input->post("memo");
                $value = array(
                    'name' => $name,
                    'memo' => $memo,
                    'modifytime' => TIME
                );
                $value['id'] = $this->db->update("{pre}account", $value, array('id' => $id));
                $balance = $balance - $account['balance'];
                $this->account->update_user_account($id, $balance, "编辑账户[{$value['name']}],[余额变更],ID[{$id}]");
                $result = array('info' => '提交成功！', 'status' => 'y', 'data' => $value);
                echo json_encode($result);
                exit;
            }
            $this->view->assign("show", $account);
            render("account/edit");
        } else {
            
        }
    }

    function del($id) {
        $account = $this->account->get_account($id);
        if ($account) {
            if (!$account['system']) {
                //将流水记录置为0
                $this->db->query("UPDATE {pre}bill SET account_id=0 WHERE account_id = '{$id}' AND user_id='{$this->auth->user_id}'");
                $this->account->update_user_account($account['id'], -$account['balance'], "删除账户[{$account['name']}],ID[{$account['id']}]");
                //删除分类
                $this->db->query("DELETE FROM {pre}account WHERE id = '{$id}' AND user_id='{$this->auth->user_id}'");
                $code = 0;
                $content = '删除成功';
            } else {
                $code = -2;
                $content = "系统默认帐户不能删除和修改";
            }
        } else {
            $code = -1;
            $content = '帐户未找到';
        }
        json_output($code, $content);
    }

}